Completed
Push — master ( e7fdc6...938c77 )
by Andres
39s
created

elements.js ➔ ... ➔ ct.elementClass   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 3
dl 0
loc 9
rs 9.6666
nop 1
1
/**
2
 elements
3
 Component that handles the periodic table tab.
4
 It includes the logic to purchase and display elements.
5
6
 @namespace Components
7
 */
8
'use strict';
9
10
angular.module('game').component('elements', {
11
  templateUrl: 'views/elements.html',
12
  controller: ['$timeout', 'state', 'data', 'util', elements],
13
  controllerAs: 'ct'
14
});
15
16
function elements($timeout, state, data, util) {
17
  let ct = this;
18
  ct.state = state;
19
  ct.data = data;
20
  ct.util = util;
21
  ct.outcome = {};
22
  ct.keys = Object.keys;
23
  ct.buyAmount = [1, 10, 25, 100, 1000];
24
25
  ct.getChance = function(element) {
26
    let bonus = 0;
27
    for(let resource of data.elements[element].includes){
28
      bonus += (state.player.statistics.all_time[resource] || 0)*data.constants.ELEMENT_CHANCE_BONUS;
29
    }
30
    console.log(bonus, state.player.statistics.all_time['16O'], data.constants.ELEMENT_CHANCE_BONUS, element)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
31
    let singleChance = data.elements[element].abundance*(1+bonus);
32
    let chance = 1 - Math.pow(Math.max(0, 1-singleChance), Math.min(state.player.resources.dark_matter.number, ct.buyAmount[state.player.options.elementBuyIndex]));
33
34
    return Math.min(1, chance);
35
  };
36
37
  ct.buyElement = function (element) {
38
    if (state.player.elements[element]) {
39
      return;
40
    }
41
    if(Math.random() < ct.getChance(element)){
42
      state.player.elements[element] = true;
43
      state.player.exotic_upgrades[element] = {};
44
      for(let up in data.exotic_upgrades){
45
        state.player.exotic_upgrades[element][up] = false;
46
      }
47
      state.player.elements_unlocked++;
48
      ct.outcome[element] = 'Success';
49
    }else{
50
      ct.outcome[element] = 'Fail';
51
    }
52
    state.player.resources.dark_matter.number -= Math.min(state.player.resources.dark_matter.number, ct.buyAmount[state.player.options.elementBuyIndex]);
53
54
    util.delayedExec(performance.now(),performance.now(), 1000, () => ct.clearMessage(element));
0 ignored issues
show
Bug introduced by
The variable performance seems to be never declared. If this is a global, consider adding a /** global: performance */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
55
  };
56
57
  ct.clearMessage = function (element) {
58
    ct.outcome[element] = '';
59
  };
60
61
  /* This function returns the class that determines on which
62
  colour an element card */
63
  ct.elementClass = function (element) {
64
    if (state.player.elements[element]) {
65
      return 'element_purchased';
66
    }
67
    if (isElementAvailable(element)) {
68
      return 'element_available';
69
    }
70
    return 'element_unavailable';
71
  };
72
73
  function isElementAvailable(element) {
74
    for(let resource of data.elements[element].includes){
75
      if(state.player.resources[resource].unlocked){
76
        return true;
77
      }
78
    }
79
    return false;
80
  }
81
82
  /* This function returns the class that determines the secondary
83
  colour of an element card */
84
  ct.elementSecondaryClass = function (element) {
85
    return ct.elementClass(element) + '_dark';
86
  };
87
}
88